package com.javalearn.gulimail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.javalearn.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121694470";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxKrbLcOmdznPlo5qQP/uOzdz+RY95ykl0mfjs5b6m3KswhaMcX9+qwa7pSmdRfbelykbBEI48S/2CQBoTyZSpNrrm7rqVEuMUHoShrMNAUyFJKqGgkpvm0hz7rGOuYQp5M3L5AtsO6OIxD71cM0h6FYQJygXYFz86Wo52CVQdOw/ZIeMrq7agPcsZtsGNnuFI+koGqhquGodLCG00dE3dI+Kc+NV3cByWrUgtk/faxnf240ZoGnCc9N9LAR2xYUUtmU0P7uL6DfJBVa93oT4bfIcBM1jJhREmbb9TP4cBaX8g+/VVdzC25/4FKMv2PuMGieG+k8haN4EIlfP2bUFRAgMBAAECggEAP6AHZah3PaUXGjFXINbZkL99zhNNI3/yQV3Kd8lKXsKA+DwGB/qmaLyGBGIkGK/9dBzgrBU1/5GKyWwb8V9DdEGjfavedgsCYgdTunxA4qj4Gz6kLPZ55rsujXOrmMRg7oX7lFHiOciI8vWkG6iPLsjrUlirIJ+4pUxAPUO8YYJpOIgXwU7X5+uSeS3Y8Q/ff665v/DSzY0m/gqK1epkDqS5Ybib7fY0j4jGTa2qYyPzrShChzt9ajRtLTx/0LDnPvzkAZI4gb8048Z0iJSyS8x8bAOg3u1TTKMdMFEvNMKVDIJxFhC41KkHupsh7m0NjloBqTnFnRj5ajUD68MJwQKBgQD9UOTlVO3S4hjemoGQ1CVjR1J3UKsliHmUnC/30B5buwrCLkcgO7H9l9dGwsWkZB7UYzFGFS40Pl0qtrYM5OP4CMFHCDhob8/x6OD0tGrEEO97EHnq+cyFIL+dVeJKpljPAk5NnWbGKld+BZVd1sxxTttS/Btbp1tBtg5C+g8h1wKBgQCzC0T+bsX4B+VKEvTzIRnxZN9pFqRudubb7+qiopUCve9FmuBftmlXJSbd2Dp8WiG8GnCRpULaU/Tn5oE4rrjoYYknoohsv5X9LjeN33rAsUlCUdVOzlknUuFdeJiV6suqNnBsG5cg4BRH67SIpUnDvHj0tPIVyEgTbpDvdF+hFwKBgCS2RpYd+uGnNud8CO8Ob+S1f3QfUs57uvMYt7L4jI3Ky8Jcpp47SI5yJRbyP2aVxAoP3iJEC5AywZnhJP01ZvyZeDltLY+lNMPgdknApACKW8rAFotBbKyWlG6e9c7P0OsfcG27NBvgIxH2iTEyYirBvPOH6gcceBNL6UE3sF29AoGAEXLwt2cuTfzfALgWPTelopv1FfXJ8qYN27NnB5CWuxytm/f7E/jgDUkmn4o6MPJLxVxCSj9z43k5LfyN7WtBxAv1ZPJ3HzoQsQr3CRGVkf/1Jsx44GRF1gpCwmxFWp72WFFT5YVO1jpgX53+Cy8ooDXXB+o2iZ7qP1KQN3X89+MCgYB6YNHg0eq7KA/rFgoEh8PoubkoY5n4g20zTcTJBaaDAfxQ/lj+kn9xVsd5y+fRGFDR78adiEz9Blv9R7piaPuaEGgxIaidZxCBaemdBB45FB0FV3w6VowYBrdWBx8RshhboEvpUcSTh1sV71RP/bDNIMKR1rOGGdtYo6vXcfD3SA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoZ++c/Lx/JOVrp2fhLswoXqTAaD4SBbOWmXMNkQtfB5DbN+B5rz6yXc4MvJO84gajp7wLEjnkkGMlAYl1r/skVW8ELVfRy9AKscprKedie09tdaC7ldq+UoF0maQ0QubKDSygbLEdgXG79J9oNf1qN+oou4lOHKkLDrpIyZMBcF5ZB7MxheyhqZdUW31w+CyMxQY9vG3w3fP+oy+KVqCASHHB72QdK5QNKJEz6whQhR6uTToz2VoOaDtF/qPYjTwOzRhEmEbfWN1vwLBTlfzYYuiqmWjJlfS3q0l7Zy7Ie+QFlnZaUJfMD+frPUMKeoVYghRmI/srXAqbdkb48VAjwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "https://63h8m78353.imdo.co/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "https://member.gulimail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
